using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Speedball2 {
  public class TextPanel {
    private static Dictionary<TextEnum, Text> _texts;
    public TextEnum _text;
    public int _unk01;

    public TextPanel () {
      _text = TextEnum.None;
      _unk01 = 0;
    }

    static TextPanel () {
      _texts = new Dictionary<TextEnum, Text> ();
      AddText (TextEnum.Replay);
      AddText (TextEnum.HalfTime);
      AddText (TextEnum.MatchOver);
      AddText (TextEnum.GameOver);
      AddText (TextEnum.Team1Scored);
      AddText (TextEnum.Team2Scored);
      AddText (TextEnum.Injured);
      AddText (TextEnum.OwnGoal);
      AddText (TextEnum.Goal);
      AddText (TextEnum.VS);
      AddText (TextEnum.Player1Full);
      AddText (TextEnum.Player1Short);
    }

    public void DrawTextGeneric (Match match, Graphics g) {
      g.DrawImage (SpriteManager.GetBitmap (BitmapEnum.PanelBackground), 16, 8);

      DrawText (g, _text);
      if ((_text == TextEnum.HalfTime) ||
          (_text == TextEnum.VS)) {
        if (_text == TextEnum.HalfTime)
          DrawText (g, TextEnum.VS);
        Text textTeam1 = match._team1._textShortName;
        textTeam1.Bytes [1] = 6;
        DrawText (g, textTeam1);
        if (match.GameType != GameTypeEnum.Practice) {
          Text textTeam2 = match._team2._textShortName;
          textTeam2.Bytes [1] = 12;
          DrawText (g, textTeam2);
        }
      }
      else if (_text == TextEnum.Team1Scored) {
        Text textTeam1 = match._team1._textShortName;
        textTeam1.Bytes [1] = 7;
        DrawText (g, textTeam1);
        _unk01--;
      }
      else if (_text == TextEnum.Team2Scored) {
        Text textTeam2 = match._team2._textShortName;
        textTeam2.Bytes [1] = 7;
        DrawText (g, textTeam2);
        _unk01--;
      }
    }

    public void DrawText (Graphics g, TextEnum text) {
      DrawText (g, GetText (text));
    }

    public void DrawText (Graphics g, Text text) {
      List<Bitmap> bitmaps = SpriteManager.GetBank (BankEnum.DmndfontSpr);
      Point xy = new Point (-1, -1);
      for (int i = 0; i < text.Bytes.Length; i++) {
        byte c = text.Bytes [i];
        if (c == ' ') {
          if ((xy.X == -1) || (xy.Y == -1))
            throw new Exception ();
          xy.X++;
        }
        else if (c < ' ') {
          xy.X = c;
          xy.Y = text.Bytes [++i];
        }
        else {
          g.DrawImage (bitmaps [c - 'A'], xy.X * 8, xy.Y * 4);
          xy.X++;
        }
      }
    }

    public static Text GetText (TextEnum text) {
      if (!_texts.ContainsKey (text))
        throw new Exception ();
      return (_texts [text]);
    }

    private static void AddText (TextEnum text) {
      _texts [text] = new Text (Game.AtariDisk, Game.AtariRamToDisk ((int) text));
    }
  }

  public enum TextEnum {
    None,
    Replay = 0xEB8,
    HalfTime = 0xEC3,
    MatchOver = 0xEDC,
    GameOver = 0xEFE,
    Team1Scored = 0xF0B,
    Team2Scored = 0xF16,
    Injured = 0xF21,
    OwnGoal = 0xF38,
    Goal = 0xF44,
    VS = 0xF4F,
    Player1Full = 0x61B6,
    Player1Short = 0x62B4
  }
}
